mylinkfandomcom-20200214-history
IosIphoneProgrammingLearningMemo
=UI Programming= Backgrounds * Swift Learning Memo * OReilly - Programming iOS 9 ** a book with coding for UI ** covers Controller and View ** does NOT cover Model * Apress - Beginning iPhone Development with Swift 2 (UI programming via xCode IDE) * UIKit - Apple doc click ** Udacity UIKit Fundamentals click Concept # AppDelegate.swift - @UIApplicationMain - main entry ## initialise self.window ## initialise first view controller self.window!.rootViewController = RootViewController() ## makes a key window self.window!.makeKeyWindow() # First view controller presents a second view controller ## self.presentViewController(SecondViewController( nibName: "SecondViewController", bundle: nil), animated:true, completion:nil) Model-View-Controller Paradigm MVC is heavy used for iOS programming, * Model: The classes that hold your application's data. * View: Made up of the windows, controls, and other elements that the user can see and interact with. * Controller: The code that binds together the model and view. It contains the application logic that decides how to handle the user’s inputs. See also apple mvc MVVM Paradigm http://teehanlax.com.s3.amazonaws.com/wordpress/wp-content/uploads/mvvm1.png The view and view controller become formally connected; we treat them as one. See also model view view-model for ios AppDelegate Tbd Outlet and Actions @IBOutlet Its sole purpose is to act as a hint to tell Xcode that this is a property that we're going to want to connect to an object in a storyboard or nib file. @IBOutlet weak var myButton2: UIButton! Tbd @IBAction This tag tells Interface Builder that this method can be triggered by a control in a storyboard or nib file. @IBAction func doSomething(sender: UIButton) {} @IBAction func doSomething() {} Tbd ViewController * Apple's doc uiviewcontroller click * A view controller is an instance of UIViewController. * A view controller manages a single view. * A view controller has no view when it first comes into existence initially. * Other controllers ** Tab bar controller ** Navigation controller ** Page view controller ** Container view controller/s ViewController Lifecycle ViewController Gets View * The view may be created in the view controller’s own code, manually. * The view may be created as an empty generic view, automatically. * The view may be created in its own separate nib. * The view may be created in a nib, which is the same nib from which the view controller itself is instantiated. Programmatically * To supply a UIViewController’s view manually - implement its loadView method. ** Obtain an instance of UIView (or a subclass of UIView) and assign it to self.view. Nib Create a nib file named MyNib Tbd let theRVC = RootViewController(nibName:"MyNib", bundle:nil) self.window!.rootViewController = theRVC Storyboard In root view controller, override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. blueViewController = storyboard?.instantiateViewControllerWithIdentifier("Blue") as! BlueViewController blueViewController.view.frame = view.frame switchViewController(from: nil, to: blueViewController) Presenting Another ViewController * presentViewController:animated:completion: * dismissViewControllerAnimated:completion: Tbd Passing Data to ViewController Tbd let svc = SecondViewController(nibName: "SecondViewController", bundle: nil) svc.data = "This is very important data!" // data to be passed on svc.delegate = self self.presentViewController(svc, animated:true, completion:nil) Lifetime Events Tbd Memory Tbd didReceiveMemoryWarning State Restoration Tbd application:shouldSaveApplicationState: application:shouldRestoreApplicationState: =Persistent Storage= Tbd do { let fm = NSFileManager() let docsurl = try fm.URLForDirectory( .DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false) // use docsurl here } catch { // examine error here } =Networking= Tbd =Threading= Tbd =Project Structure= Tbd Targets Tbd Nib Managements Tbd =Appendix= Add UI Programmatically UIButton code // var button = UIButton.buttonWithType(UIButtonType.System) as UIButton let button = UIButton(type: .System) // let preferred over var here button.frame = CGRectMake(100, 100, 100, 50) button.backgroundColor = UIColor.greenColor() button.setTitle("Button", forState: UIControlState.Normal) button.addTarget(self, action: "Action:", forControlEvents: UIControlEvents.TouchUpInside) self.view.addSubview(button) UILabel Code var label: UILabel = UILabel() label.frame = CGRectMake(50, 50, 200, 21) label.backgroundColor = UIColor.blackColor() label.textColor = UIColor.whiteColor() label.textAlignment = NSTextAlignment.Center label.text = "test label" self.view.addSubview(label) UITextField code var txtField: UITextField = UITextField() txtField.frame = CGRectMake(50, 70, 200, 30) txtField.backgroundColor = UIColor.grayColor() self.view.addSubview(txtField) Origin from click TableView @IBOutlet weak var myTable: UITableView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. myTable.delegate = self myTable.dataSource = self =Appendix= Tbd